package com.example.test;

import com.example.mapper.UserMapper;
import com.example.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class Mptest {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void test1() {
        List<User> list = userMapper.selectList(null);
        System.out.println("users:" + list);
    }

    @Test
    public void test2() {
        User user = new User();
        user.setId(1);
        user.setName("Jone");
        user.setEmail("test1@baomidou.com");
        user.setAge(17);
        int ret = userMapper.insert(user);
        System.out.println("ret = " + ret);

    }


    @Test
    public void delete() {
        // 根据id删除
        int ret1 = userMapper.deleteById(1912647681);
        System.out.println("ret = " + ret1);

        // deleteByMap
        Map map = new HashMap();
        map.put("age", 18); // where age = 18
        // map.put("name", "xxx") // ~~~ and name = xxxx;
        int ret2 = userMapper.deleteByMap(map);

        System.out.println("ret = " + ret2);


        // wapper
        // userMapper.delete();
    }

    @Test
    public void update() {
        // 当属性值为null的时候 不修改
        User user = new User();
        user.setId(1);
        user.setAge(30);
        // 根据id进行修改
        int ret = userMapper.updateById(user);
        System.out.println(ret);


        // 修改所有人的年龄为20
        User user1 = new User();
        user1.setAge(20);
        int ret1 = userMapper.update(user1, null);
        System.out.println(ret1);
    }

    @Test
    public void select_test() {
        User user = userMapper.selectById(1);
        System.out.println(user);

        List<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(4);
        List<User> users = userMapper.selectBatchIds(list);
        System.out.println(users);
    }

    @Test
    public void test() {
        // 全表删除
        userMapper.delete(null);
    }
}
